<template>
  <div class="app-container">

    <el-form :model="form" ref="form" label-width="100px" v-loading="formLoading" :rules="rules">
      <el-form-item label="行政班名："  prop="name">
        <el-input v-model="form.name"></el-input>
      </el-form-item>
      <el-form-item label="所属院系：" prop="department">
        <el-input v-model="form.department"></el-input>
      </el-form-item>
      <el-form-item label="专业："  prop="major">
        <el-input v-model="form.major"></el-input>
      </el-form-item>
      <el-form-item label="班长："  prop="monitor">
        <el-select v-model="form.monitor" filterable remote reserve-keyword placeholder="输入姓名或学号" :remote-method="refreshMonitorOptions" :loading="monitorOptionsLoading">
          <el-option
            v-for="item in monitorOptions"
            :key="item.id"
            :label="item.name + ' '+ item.sno"
            :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>
      <!--<el-form-item label="辅导员："  prop="counsellor">
        <el-select v-model="form.counsellor" filterable placeholder="请选择" filter-method="refreshCounsellorOptions">
          <el-option
            v-for="item in counsellorOptions"
            :key="item.id"
            :label="item.name + ' '+ item.sno"
            :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>-->
      <el-form-item>
        <el-button type="primary" @click="submitForm">提交</el-button>
        <el-button @click="resetForm">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { mapGetters, mapState, mapActions } from 'vuex'
import administrationApi from '@/api/administration'

export default {
  data () {
    return {
      form: {
        id: null,
        name: '',
        department: '',
        major: '',
        monitor: undefined,
        counsellor: undefined
      },
      monitorOptionsLoading: false,
      monitorOptions: [],
      counsellorOptions: [],
      formLoading: false,
      listStudentQuery: {
        pageIndex: 1,
        pageSize: 5,
        keyWord: ''
      },
      rules: {
        name: [
          { required: true, message: '请输入行政班名', trigger: 'blur' }
        ],
        department: [
          { required: true, message: '请输入所属院系', trigger: 'blur' }
        ],
        major: [
          { required: true, message: '请选择专业', trigger: 'change' }
        ]
      }
    }
  },
  created () {
    let id = this.$route.query.id
    let _this = this
    if (id && parseInt(id) !== 0) {
      _this.formLoading = true
      administrationApi.selectClass(id).then(re => {
        _this.form = re.response
        _this.formLoading = false
      })
    }
  },
  methods: {
    submitForm () {
      let _this = this
      this.$refs.form.validate((valid) => {
        if (valid) {
          this.formLoading = true
          administrationApi.createClass(this.form).then(data => {
            if (data.code === 1) {
              _this.$message.success(data.message)
              _this.delCurrentView(_this).then(() => {
                _this.$router.push('/administration/adClass/list')
              })
            } else {
              _this.$message.error(data.message)
              _this.formLoading = false
            }
          }).catch(e => {
            _this.formLoading = false
          })
        } else {
          return false
        }
      })
    },
    resetForm () {
      this.$refs['form'].resetFields()
    },
    refreshMonitorOptions (query) {
      if (query !== '') {
        this.monitorOptionsLoading = true
        this.listStudentQuery.keyWord = query
        administrationApi.listStudent(this.listStudentQuery).then(response => {
          this.monitorOptions = response.response.list
          this.monitorOptionsLoading = false
        }).catch(() => {
          this.monitorOptions = []
          this.monitorOptionsLoading = false
        })
      } else {
        this.monitorOptions = []
      }
    },
    ...mapActions('tagsView', { delCurrentView: 'delCurrentView' })
  },
  computed: {
    ...mapGetters('enumItem', [
      'enumFormat'
    ]),
    ...mapState('enumItem', {
      sexEnum: state => state.user.sexEnum,
      roleEnum: state => state.user.roleEnum,
      statusEnum: state => state.user.statusEnum,
      levelEnum: state => state.user.levelEnum
    })
  }
}
</script>
